# The website says the Amcerican Community Survey (ACS) has their most recent data set as 2014-2018. The ACS includesvariables on social, economic, housing, and demographic aspects. It has 25,070 variables.
var <- load_variables(year = 2018, dataset = "acs5")
# Finding the demographic variables and renaming them in a list
demo_varsacs5 <- c(
associates = "B15003_021",
bachelors = "B15003_022",
poverty = "B17001_002",
female = "B01001_026",
male = "B01001_002",
diff_stateHouse = "B07013_014",
diff_stateRent = "B07013_015",
diff_countyHouse = "B07013_011",
diff_countyRent = "B07013_012",
house_val = "B25107_001",
self_income = "B19053_002",
#population = "B01003_001", #*
private_ins = "B27002_001", #*
health_ins = 'B27001_001',
public_ins = "B27003_001",
income = "B06011_001",
male_car = "B08006_019",
female_car = "B08006_036",
pharm = "B24115_202"
)
Census tracts are small, relatively permanent statistical subdivisions of counties designed to be relatively homogeneous units with respect to population characteristics, economic status, and living conditions. They are updated by local participants prior to each decennial census as part of the Census Bureau’s Participant Statistical Areas Program. Census tracts generally have a population size between 1,200 and 8,000 people, with an optimum size of 4,000 people. Census tract boundaries never cross county lines
# We have to use tracts so that we can get geographical information using sf are leaflet
nj_demo_tracts <- get_acs(
state = "NJ",
geography = "tract",
variables = demo_varsacs5,
# Use 2018 as year for more recent data
year = 2018,
geometry = TRUE,
keep_geo_vars = TRUE
) %>%
# 4326 is most common map form
st_transform(4326)
# Function to create tracts
tracts_func <- function(data, county_n, var_n) {
data %>%
# Filters the county
filter(COUNTYFP == county_n) %>%
# Filters for the variable from our variable list
filter(variable == var_n)
}
#Function to pull variable from all tracts in NJ
alco_func <- function(var_n){
nj_demo_tracts %>%
filter(COUNTYFP == "001"
| COUNTYFP == "003"
| COUNTYFP == "005"
| COUNTYFP == "009"
| COUNTYFP == "007"
| COUNTYFP == "011"
| COUNTYFP == "013"
| COUNTYFP == "015"
| COUNTYFP == "017"
| COUNTYFP == "019"
| COUNTYFP == "021"
| COUNTYFP == "023"
| COUNTYFP == "025"
| COUNTYFP == "027"
| COUNTYFP == "029"
| COUNTYFP == "031"
| COUNTYFP == "033"
| COUNTYFP == "035"
| COUNTYFP == "037"
| COUNTYFP == "039"
| COUNTYFP == "041") %>%
filter(variable == var_n)
}
#function to color the map. takes in string color pallette and mapping variable
pal_func <- function(color, var_name){
colorNumeric(
palette = color,
domain = var_name$estimate,
reverse = TRUE
)
}
nj_pop <- nj_demo_tracts %>%
filter(COUNTYFP == "001"
| COUNTYFP == "003"
| COUNTYFP == "005"
| COUNTYFP == "009"
| COUNTYFP == "007"
| COUNTYFP == "011"
| COUNTYFP == "013"
| COUNTYFP == "015"
| COUNTYFP == "017"
| COUNTYFP == "019"
| COUNTYFP == "021"
| COUNTYFP == "023"
| COUNTYFP == "025"
| COUNTYFP == "027"
| COUNTYFP == "029"
| COUNTYFP == "031"
| COUNTYFP == "033"
| COUNTYFP == "035"
| COUNTYFP == "037"
| COUNTYFP == "039"
| COUNTYFP == "041") %>%
select(-(moe)) %>%
spread(key = variable, value = estimate) %>%
mutate(estimate = female + male)
nj_pop
drop = c("associates",
"bachelors",
"poverty",
"diff_stateHouse",
"diff_stateRent",
"diff_countyHouse",
"diff_countyRent",
"house_val",
"self_income",
"health_ins",
"income",
"male_car",
"female_car",
"pharm",
"female",
"male",
"population")
nj_pop <- nj_pop[,!(names(nj_pop) %in% drop)]
car <- nj_demo_tracts %>%
filter(COUNTYFP == "001"
| COUNTYFP == "003"
| COUNTYFP == "005"
| COUNTYFP == "009"
| COUNTYFP == "007"
| COUNTYFP == "011"
| COUNTYFP == "013"
| COUNTYFP == "015"
| COUNTYFP == "017"
| COUNTYFP == "019"
| COUNTYFP == "021"
| COUNTYFP == "023"
| COUNTYFP == "025"
| COUNTYFP == "027"
| COUNTYFP == "029"
| COUNTYFP == "031"
| COUNTYFP == "033"
| COUNTYFP == "035"
| COUNTYFP == "037"
| COUNTYFP == "039"
| COUNTYFP == "041") %>%
select(-(moe)) %>%
spread(key = variable, value = estimate) %>%
mutate(estimate = female_car + male_car)
car
drop = c("associates",
"bachelors",
"poverty",
"diff_stateHouse",
"diff_stateRent",
"diff_countyHouse",
"diff_countyRent",
"house_val",
"self_income",
"health_ins",
"income",
"male_car",
"female_car",
"pharm",
"female",
"male",
"population")
car <- nj_pop[,!(names(nj_pop) %in% drop)]
# Function for creating leaflet map
map_func <- function(data, title, title2) {
data %>%
leaflet(width = "100%") %>%
addProviderTiles(provider = "CartoDB.Positron") %>%
addPolygons(
label = ~ str_extract(NAME.y, "^([^,]*)"),
stroke = FALSE,
smoothFactor = 0,
fillOpacity = 0.7,
color = ~ pal(estimate)
) %>%
addLegend("bottomright",
pal = pal,
values = ~estimate,
title = title,
opacity = 1
) %>%
addControl(title2, position = "topright")
}
# Read in Museums
m <- read.csv(file = "~/Desktop/UBS/m.csv")
m <- subset(m, m$State..Administrative.Location. == "NJ" & m$State.Code..FIPS. == 34)
head(m)
poverty_map <- alco_func("poverty")
poverty_map
# Creating pallette to use with leaflet
pal <- pal_func("viridis", poverty_map)
map_func(poverty_map, "Number of People (Ones)", "Poverty in New Jersey")
bachelors <- alco_func("bachelors")
bachelors
# Creating pallette to use with leaflet
pal <- pal_func("viridis", bachelors)
map_func(bachelors, "Number of People (Ones)", "Bachelors Degrees in New Jersey")
associates <- alco_func("associates")
associates
# Creating pallette to use with leaflet
pal <- pal_func("viridis", associates)
map_func(associates, "Number of People (Ones)", "Bachelors Degrees in New Jersey")
house_val <- alco_func("house_val")
house_val
# Creating pallette to use with leaflet
pal <- pal_func("viridis", house_val)
map_func(house_val, "House Value (Dollars)", "Housing Value in New Jersey")
# Creating pallette to use with leaflet
pal <- pal_func("viridis", nj_pop)
map_func(nj_pop, "Nubmer of People (Ones)", "Population in New Jersey")
health_ins <- alco_func("health_ins")
health_ins
# Creating pallette to use with leaflet
pal <- pal_func("viridis", health_ins)
map_func(health_ins, "Number of People (Ones)", "Health Insurance in New Jersey")
income <- alco_func("income")
income
# Creating pallette to use with leaflet
pal <- pal_func("viridis", income)
#map_func(income, "income", "income")
income_map <- income %>%
leaflet(width = "100%") %>%
addProviderTiles(provider = "CartoDB.Positron") %>%
addPolygons(
label = ~ str_extract(NAME.y, "^([^,]*)"),
stroke = FALSE,
smoothFactor = 0,
fillOpacity = 0.7,
color = ~ pal(estimate)
) %>%
addLegend("bottomright",
pal = pal,
values = ~estimate,
title = "Income (Dollars)",
opacity = 1
) %>%
# Adding the museums
addControl("Median Income in New Jersey", position = "topright") %>%
addCircleMarkers(
lng= m$Longitude, lat= m$Latitude, radius = 1,
label= m$Museum.Name,
labelOptions = labelOptions(direction = 'bottom',
offset=c(0,15)))
income_map
# Creating pallette to use with leaflet
pal <- pal_func("viridis", car)
map_func(car, "Number of Cars (Ones)", "Number of Cars Owned in New Jersey")
self_income <- alco_func("self_income")
self_income
# Creating pallette to use with leaflet
pal <- pal_func("viridis", self_income)
map_func(self_income, "Number of People (Ones)", "Number of People Who Reported to Having Self-Income")
diff_stateHouse <- alco_func("diff_stateHouse")
diff_stateHouse
# Creating pallette to use with leaflet
pal <- pal_func("viridis", diff_stateHouse)
map_func(diff_stateHouse, "Number of People (Ones)", "Number of People who Moved from a Different State and Now Live in a House")
diff_stateRent <- alco_func("diff_stateRent")
diff_stateRent
# Creating pallette to use with leaflet
pal <- pal_func("viridis", diff_stateRent)
map_func(diff_stateRent, "Number of People (Ones)", "Number of People who Moved from a Different State and Now Live in a Rental Unit")
diff_countyHouse <- alco_func("diff_countyHouse")
diff_countyHouse
# Creating pallette to use with leaflet
pal <- pal_func("viridis", diff_countyHouse)
map_func(diff_countyHouse, "Number of People (Ones)", "Number of People who Moved from a Different County and Now Live in a House")
diff_countyRent <- alco_func("diff_countyRent")
diff_countyRent
# Creating pallette to use with leaflet
pal <- pal_func("viridis", diff_countyRent)
map_func(diff_countyRent, "Number of People (Ones)", "Number of People who Moved from a Different County and Now Live in a Rental Unit")